package com.yksj.healthtalk.ui.doctorstation.service;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 服务内容详细
 * @author zhao
 *
 */
public class DoctorServiceDescriptionActivity extends BaseFragmentActivity implements OnClickListener{
	
	DataListAdapter mAdapter;
	ListView mListView;
	String mServiceTypeId;
	String mCustomerId;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_service_description_list_layout);
		
		if(arg0 != null){
			mCustomerId = arg0.getString("mCustomerId");
			mServiceTypeId = arg0.getString("mServiceTypeId");
		}else{
			mServiceTypeId = getIntent().getStringExtra("SERVICE_TYPE_ID");
			mCustomerId = getIntent().getStringExtra("CUSTOMER_ID");
		}
		
		initUI();
		initData();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("mServiceTypeId",mServiceTypeId);
		outState.putString("mCustomerId",mCustomerId);
	}
	
	private void initUI(){
		initTitle();
		setBackBtn("返回",this);
		titleTextV.setText("服务内容");
		setRightBtnYellowBg("添加",this);
		mListView = (ListView)findViewById(android.R.id.list);
		mAdapter = new DataListAdapter();
		mListView.setAdapter(mAdapter = new DataListAdapter());
	}
	
	/**
	 * 
	 * 查询服务内容介绍
	 * 
	 */
	private void initData(){
		RequestParams paramsm = new RequestParams();
		paramsm.put("Type", "queryEditorDoctorServer");
		paramsm.put("SERVICE_TYPE_ID", mServiceTypeId);
		paramsm.put("CUSTOMER_ID",mCustomerId);
		HttpRestClient.doHttpServiceSetServlet320(paramsm,new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONArray response) {
				mAdapter.onDataChange(response);
			}
		});
	}
	
	/**
	 * 
	 * 删除服务内容项
	 * @param jsonObject
	 */
	private void onDeleteContent(final JSONObject jsonObject){
		RequestParams paramsm = new RequestParams();
		paramsm.put("Type", "deleteDoctorServler");
		paramsm.put("SERVICE_CONTENT_ID",jsonObject.getString("SERVICE_CONTENT_ID"));
		paramsm.put("CUSTOMER_ID",mCustomerId);
		HttpRestClient.doHttpServiceSetServlet320(paramsm,new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode,JSONObject response) {
				//失败
				if(response.containsKey("error_message")){
					ToastUtil.showLong(getApplicationContext(),response.getString("error_message"));
				}
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2://添加
			
			break;
		}
	}
	
	class DataListAdapter extends BaseAdapter{
		JSONArray mArray = new JSONArray();
		
		private void onDataChange(JSONArray array){
			this.mArray = array;
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			return mArray.size();
		}

		@Override
		public JSONObject getItem(int position) {
			return mArray.getJSONObject(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final JSONObject jsonObject = getItem(position);
			final ViewHolder holder; 
			if(convertView == null){
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(R.layout.service_centent_item,null);
				holder.checkBox = (CheckBox)convertView.findViewById(R.id.check);
				holder.titleTextView = (TextView)convertView.findViewById(R.id.title);
				holder.contentTextView = (TextView)convertView.findViewById(R.id.content);
				holder.deleteImageView = (ImageView)convertView.findViewById(R.id.delete_btn);
				holder.arrowButton = (CheckBox)convertView.findViewById(R.id.arrow);
			}else{
				holder = (ViewHolder)convertView.getTag();
			}
			int selected = jsonObject.getIntValue("ISSELECT");
			String type = jsonObject.getString("SERVICE_TYPE");//
			if("System".equals(type)){//系统不可删除
				holder.deleteImageView.setVisibility(View.GONE);
			}else{
				holder.deleteImageView.setVisibility(View.VISIBLE);
			}
			String title = jsonObject.getString("SERVICE_CONTENT");
			String content = jsonObject.getString("SERVICE_DESC");
			holder.titleTextView.setText(title);
			holder.checkBox.setChecked((selected == 1?true:false));
			//删除
			holder.deleteImageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onDeleteContent(jsonObject);
				}
			});
			//是否展开
			boolean isExpend = jsonObject.getBooleanValue("isExpend");
			holder.arrowButton.setChecked(isExpend);
			holder.arrowButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					CheckBox box = (CheckBox)v;
					jsonObject.put("isExpend",box.isChecked());
				}
			});
			//是否展开
			if(holder.arrowButton.isChecked()){
				holder.contentTextView.setText(content);
			}else{
				int len = content.length();
				if(len > 200){
					len = 200;
					holder.contentTextView.setText(content.subSequence(0,len));
				}else{
					holder.contentTextView.setText(content);
				}
			}
			return convertView;
		}
	}
	
	private static class ViewHolder{
		CheckBox checkBox;
		TextView titleTextView;
		TextView contentTextView;
		CheckBox arrowButton;
		ImageView deleteImageView;
	}
	
}
